// Functions associated with the Text class
//-----------------

// More information at http://www.toymaker.info/Games/html/text.html

//-----------------
// Includes libraries and other files
#include "Text.h"

//-----------------

//-----------------
//// CONSTRUCTOR - Text
Text::Text( LPDIRECT3DDEVICE9 device, int height, int width) {
	create_font( device, height, width);

	// set colour and rect to prevent problems
	colour_ = D3DCOLOR_ARGB( 255, 0, 0, 0);
	set_rect( 0, 0, 10, 10);
}

//// DESTRUCTOR - Text
Text::~Text() {
	font_->Release();
}
//-----------------

//-----------------
//// FUNCTION - create_font
// sets the font that will be used
void Text::create_font( LPDIRECT3DDEVICE9 device, int height, int width) {
	device_ = device;

	D3DXCreateFont( device_,						// D3D device
					height,							// height of the font
					width,							// width of the font
					FW_BOLD,						// weight
					1,								// miplevels, 0 = autogen mipmaps
					FALSE,							// italic
					DEFAULT_CHARSET,				// charset
					OUT_DEFAULT_PRECIS,				// output precision
					DEFAULT_QUALITY,				// quality
					DEFAULT_PITCH | FF_DONTCARE,	// pitch and family
					TEXT( "Courier"),				// face name
					&font_);						// the variable that will 'contain' all the information
}

//// FUNCTION - set_colour
// set the colour of the text by using a D3DCOLOR object
void Text::set_colour( D3DCOLOR colour) {
	colour_ = colour;
}

//// FUNCTION - set_colour
// set the colour of the text by specifying each aspect
void Text::set_colour( int a, int r, int g, int b) {
	colour_ = D3DCOLOR_ARGB( a, r, g, b);
}

//// FUNCTION - set_rect
// set the size of the 'box' that the text will go into; if the text goes outside the boundries then it simply won't be displayed
void Text::set_rect( int x, int y, int width, int height) {
	rect_.left		= x;
	rect_.top		= y;
	rect_.right		= x + width;
	rect_.bottom	= y + height;
}

//// FUNCTION - render
// output the text using the previously saved font, colour and rect dimensions
void Text::render( LPCTSTR text) {
	// Pre-conditions :- the device must exist
	assert( device_ != NULL);

	font_->DrawTextA( NULL, text, -1, &rect_, 0, colour_);
}

//// FUNCTION - render
// output the text and specify the location
void Text::render( LPCTSTR text, int x, int y, int width, int height) {
	// Pre-conditions :- the device must exist
	assert( device_ != NULL);

	set_rect( x, y, width, height);
	render( text);
}

//// FUNCTION - render
// output the text and specift the location and its colour
void Text::render( LPCTSTR text, int x, int y, int width, int height, int a, int r, int g, int b) {
	// Pre-conditions :- the device must exist
	assert( device_ != NULL);

	colour_ = D3DCOLOR_ARGB( a, r, g, b);
	set_rect( x, y, width, height);
	render( text);
}
